package com.zizy.healthexpert.Database;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;

public class MyDataBase_T_FOODS_NUTRITION {
	static final int PLACETOTAL = 115;// use for check if the table is
	// completely created
	static MyDataBase_T_FOODS_NUTRITION.DataBase myDataBaseHelper;
	private SQLiteDatabase db;// the order is name, type,latitude,longtitude,
	final static String TABLENAME = "T_FOOD_NUTRITIONS";

	public MyDataBase_T_FOODS_NUTRITION(Context context) {
		myDataBaseHelper = new MyDataBase_T_FOODS_NUTRITION.DataBase(context);
		this.db = myDataBaseHelper.getWritableDatabase();
		//initMyDataBase(db);
	}

	public void createTable(String tableName) {
		String sql = "create table if not exists "
				+ tableName
				+ " (RecNo integer primary key autoincrement, FOOD_ID integer(20) not null ,NUTRITION_ID integer(20) not null,CONTENT_VALUE integer(10)not null );";
		db.execSQL(sql);
	}

	// init database
	public void initMyDataBase(SQLiteDatabase db) {
		int placeTotal = 0;
		this.createTable(TABLENAME);
		if (this.SearchT_FOODS_NUTRITION("*") != null)
			placeTotal = this.SearchT_FOODS_NUTRITION("*").size();
		// if is completely create then return
		if (placeTotal == MyDataBase_T_FOODS_NUTRITION.PLACETOTAL)
			return;
		// or insert
		//myDataBaseHelper.deleteTable();
		this.createTable(TABLENAME);

		this.Insert(1, 1, 1, 317);

	}

	public void deleteTabel() {

	}

	// insert operating

	public void Insert(int RecNo, int FOOD_ID, int NUTRITION_ID,
			int CONTENT_VALUE) {

		this.db.execSQL(String
				.format("insert into %s (RecNo,FOOD_ID, NUTRITION_ID,CONTENT_VALUE) values('%s','%s','%s','%s')",
						TABLENAME, RecNo, FOOD_ID, NUTRITION_ID, CONTENT_VALUE));
	}

	// update operation
	public void Update(String field, Object value, String where_expression) {
		this.db.execSQL(String.format("update %s set password=%s where %s ;",
				TABLENAME, field, value, where_expression));
	}

	// Delete operation
	public void Delete(String where_expression) {
		this.db.execSQL(String.format("delete from %s  where %s ;", TABLENAME,
				where_expression));
	}

	public ArrayList<MyDataBase_T_FOODS_NUTRITION.T_FOODS_NUTRITION> SearchT_FOODS_NUTRITION(
			String selectWhat) {
		ArrayList<MyDataBase_T_FOODS_NUTRITION.T_FOODS_NUTRITION> FOODSList = new ArrayList<MyDataBase_T_FOODS_NUTRITION.T_FOODS_NUTRITION>();
		Cursor cursor = db.rawQuery("select " + selectWhat + " from "
				+ TABLENAME, null);
		while (cursor.moveToNext()) {
			int FOOD_ID = cursor.getInt(0);
			int NUTRITION_ID = cursor.getInt(1);
			int CONTENT_VALUE = cursor.getInt(2);
			// here create a Place class to add info.
			FOODSList.add(new T_FOODS_NUTRITION(   FOOD_ID, NUTRITION_ID,
					CONTENT_VALUE));

		}

		cursor.close();

		return FOODSList;
	}

	public ArrayList<MyDataBase_T_FOODS_NUTRITION.T_FOODS_NUTRITION> SearchT_FOODS_NUTRITION(
			String selectWhat, String where_expression) {
		ArrayList<MyDataBase_T_FOODS_NUTRITION.T_FOODS_NUTRITION> FOODSList = new ArrayList<MyDataBase_T_FOODS_NUTRITION.T_FOODS_NUTRITION>();
		Cursor cursor = db.rawQuery(String.format(
				"select %s from %s where %s;", selectWhat, TABLENAME,
				where_expression), null);
		while (cursor.moveToNext()) {
			int FOOD_ID = cursor.getInt(0);
            int NUTRITION_ID = cursor.getInt(1);
			double CONTENT_VALUE = cursor.getDouble(2);

			FOODSList.add(new T_FOODS_NUTRITION(   FOOD_ID, NUTRITION_ID,
					CONTENT_VALUE));

		}
		cursor.close();
		return FOODSList;
	}

	public void Close() {
		this.db.close();
	}

	public class DataBase extends DatabaseHelper {
		public DataBase(Context context) {
			super(context );

			// TODO Auto-generated constructor stub
		}

	}

	// here is a Type of Place
	public class T_FOODS_NUTRITION {

		public int FOOD_ID;
		public int NUTRITION_ID;
		public double CONENT_VALUE;

		T_FOODS_NUTRITION(   int FOOD_ID, int NUTRITION_ID,
				double CONENT_VALUE) {
			// T_FOODS(int RecNo,String locale ) {
			this.FOOD_ID = FOOD_ID;
			this.NUTRITION_ID = NUTRITION_ID;
			this.CONENT_VALUE = CONENT_VALUE;

		}

	}

}
